
# A2_plot_price_diffs
#===============================================================================

# Description: This file plots average price difference for each country and creates 
# the Figure 1 subplots

rm(list = ls())

setwd("D:/data_replication")

library("data.table")  
library("ggplot2")  
library("ggrepel")  
library("stargazer")  

output <- fread('statistics_and_graphs/import_vs_export_prices/output_files/results_diff_med.txt')
output[ , V1 := gsub(".declarant", "", V1)]
setnames(output, "V1", "declarant")
setnames(output, "V2", "diff")
const <- output$diff[nrow(output)]
const <- as.numeric(const)
output <- output[declarant != ""]
output <- output[declarant != "_cons"]
output[ , declarant := as.numeric(declarant)]
output[ , diff := as.numeric(diff)]
output[ , diff := diff + const]

gdp_data <- fread('data/gdp_per_capita/gdp_per_capita_declarant.csv')
gdp_data <- gdp_data[year == 2007]

output <- merge(output, gdp_data, by = "declarant")
output <- as.data.table(output)

iso3 <- fread('data/country_codes/ISO3_declarant_codes.csv') 
output <- merge(output, iso3, by = "declarant")
output <- as.data.table(output)

output[ , gdp_log := log(gdp_per_capita_declarant)]

plot_diff_med <- ggplot(output, aes(x=gdp_log, y=diff)) +
                geom_text(label=output$declarant_description, size = 5) +
                theme(text = element_text(size=17)) +
                geom_smooth(method=lm) +
                labs(x = "Importer GDP per capita (in logs)", y = "Diff") 


ggsave("statistics_and_graphs/import_vs_export_prices/plot_diff_med.png", plot = plot_diff_med)


# diff_med_exEU
#--------------------------------------------------------------------------------------------------------

rm(list = ls())

output <- fread('statistics_and_graphs/import_vs_export_prices/output_files/results_diff_med_exEU.txt')
output[ , V1 := gsub(".declarant", "", V1)]
setnames(output, "V1", "declarant")
setnames(output, "V2", "diff")
const <- output$diff[nrow(output)]
const <- as.numeric(const)
output <- output[declarant != ""]
output <- output[declarant != "_cons"]
output[ , declarant := as.numeric(declarant)]
output[ , diff := as.numeric(diff)]
output[ , diff := diff + const]

gdp_data <- fread('data/gdp_per_capita/gdp_per_capita_declarant.csv')
gdp_data <- gdp_data[year == 2007]

output <- merge(output, gdp_data, by = "declarant")
output <- as.data.table(output)

iso3 <- fread('data/country_codes/ISO3_declarant_codes.csv') 
output <- merge(output, iso3, by = "declarant")
output <- as.data.table(output)

output[ , gdp_log := log(gdp_per_capita_declarant)]

plot_diff_med_exEU <- ggplot(output, aes(x=gdp_log, y=diff)) +
  geom_text(label=output$declarant_description, size = 5) +
  theme(text = element_text(size=17)) +
  geom_smooth(method=lm) +
  labs(x = "Importer GDP per capita (in logs)", y = "Diff") 


ggsave("statistics_and_graphs/import_vs_export_prices/plot_diff_med_exEU.png", plot = plot_diff_med_exEU)



# diff_med_asia
#--------------------------------------------------------------------------------------------------------

rm(list = ls())

output <- fread('statistics_and_graphs/import_vs_export_prices/output_files/results_diff_med_asia.txt')
output[ , V1 := gsub(".declarant", "", V1)]
setnames(output, "V1", "declarant")
setnames(output, "V2", "diff")
const <- output$diff[nrow(output)]
const <- as.numeric(const)
output <- output[declarant != ""]
output <- output[declarant != "_cons"]
output[ , declarant := as.numeric(declarant)]
output[ , diff := as.numeric(diff)]
output[ , diff := diff + const]

gdp_data <- fread('data/gdp_per_capita/gdp_per_capita_declarant.csv')
gdp_data <- gdp_data[year == 2007]

output <- merge(output, gdp_data, by = "declarant")
output <- as.data.table(output)

iso3 <- fread('data/country_codes/ISO3_declarant_codes.csv') 
output <- merge(output, iso3, by = "declarant")
output <- as.data.table(output)

output[ , gdp_log := log(gdp_per_capita_declarant)]

plot_diff_med_asia <- ggplot(output, aes(x=gdp_log, y=diff)) +
  geom_text(label=output$declarant_description, size = 5) +
  theme(text = element_text(size=17)) +
  geom_smooth(method=lm) +
  labs(x = "Importer GDP per capita (in logs)", y = "Diff") 


ggsave("statistics_and_graphs/import_vs_export_prices/plot_diff_med_asia.png", plot = plot_diff_med_asia)



# diff_med_weighted
#--------------------------------------------------------------------------------------------------------

rm(list = ls())

output <- fread('statistics_and_graphs/import_vs_export_prices/output_files/results_diff_med_weighted.txt')
output[ , V1 := gsub(".declarant", "", V1)]
setnames(output, "V1", "declarant")
setnames(output, "V2", "diff")
const <- output$diff[nrow(output)]
const <- as.numeric(const)
output <- output[declarant != ""]
output <- output[declarant != "_cons"]
output[ , declarant := as.numeric(declarant)]
output[ , diff := as.numeric(diff)]
output[ , diff := diff + const]

gdp_data <- fread('data/gdp_per_capita/gdp_per_capita_declarant.csv')
gdp_data <- gdp_data[year == 2007]

output <- merge(output, gdp_data, by = "declarant")
output <- as.data.table(output)

iso3 <- fread('data/country_codes/ISO3_declarant_codes.csv') 
output <- merge(output, iso3, by = "declarant")
output <- as.data.table(output)

output[ , gdp_log := log(gdp_per_capita_declarant)]


lm(diff ~ gdp_log, data = output)

plot_diff_med_weighted <- ggplot(output, aes(x=gdp_log, y=diff)) +
  geom_text_repel(label=output$declarant_description, size = 5) +
  theme(text = element_text(size=17)) +
  geom_smooth(method=lm) +
  labs(x = "Importer GDP per capita (in logs)", y = "log(Domestic Price) - log(Import Price)") 


ggsave("statistics_and_graphs/import_vs_export_prices/plot_diff_med_weighted.png", plot = plot_diff_med_weighted)



# diff_med_weighted_exEU
#--------------------------------------------------------------------------------------------------------

rm(list = ls())

output <- fread('statistics_and_graphs/import_vs_export_prices/output_files/results_diff_med_weighted_exEU.txt')
output[ , V1 := gsub(".declarant", "", V1)]
setnames(output, "V1", "declarant")
setnames(output, "V2", "diff")
const <- output$diff[nrow(output)]
const <- as.numeric(const)
output <- output[declarant != ""]
output <- output[declarant != "_cons"]
output[ , declarant := as.numeric(declarant)]
output[ , diff := as.numeric(diff)]
output[ , diff := diff + const]

gdp_data <- fread('data/gdp_per_capita/gdp_per_capita_declarant.csv')
gdp_data <- gdp_data[year == 2007]

output <- merge(output, gdp_data, by = "declarant")
output <- as.data.table(output)

iso3 <- fread('data/country_codes/ISO3_declarant_codes.csv') 
output <- merge(output, iso3, by = "declarant")
output <- as.data.table(output)

output[ , gdp_log := log(gdp_per_capita_declarant)]

plot_diff_med_weighted_exEU <- ggplot(output, aes(x=gdp_log, y=diff)) +
  geom_text(label=output$declarant_description, size = 5) +
  theme(text = element_text(size=17)) +
  geom_smooth(method=lm) +
  labs(x = "Importer GDP per capita (in logs)", y = "Diff") 


ggsave("statistics_and_graphs/import_vs_export_prices/plot_diff_med_weighted_exEU.png", plot = plot_diff_med_weighted_exEU)




# diff_med_weighted_asia
#--------------------------------------------------------------------------------------------------------

rm(list = ls())

output <- fread('statistics_and_graphs/import_vs_export_prices/output_files/results_diff_med_weighted_asia.txt')
output[ , V1 := gsub(".declarant", "", V1)]
setnames(output, "V1", "declarant")
setnames(output, "V2", "diff")
const <- output$diff[nrow(output)]
const <- as.numeric(const)
output <- output[declarant != ""]
output <- output[declarant != "_cons"]
output[ , declarant := as.numeric(declarant)]
output[ , diff := as.numeric(diff)]
output[ , diff := diff + const]

gdp_data <- fread('data/gdp_per_capita/gdp_per_capita_declarant.csv')
gdp_data <- gdp_data[year == 2007]

output <- merge(output, gdp_data, by = "declarant")
output <- as.data.table(output)

iso3 <- fread('data/country_codes/ISO3_declarant_codes.csv') 
output <- merge(output, iso3, by = "declarant")
output <- as.data.table(output)

output[ , gdp_log := log(gdp_per_capita_declarant)]

plot_diff_med_weighted_asia <- ggplot(output, aes(x=gdp_log, y=diff)) +
  geom_text_repel(label=output$declarant_description, size = 5) +
  theme(text = element_text(size=17)) +
  geom_smooth(method=lm) +
  labs(x = "Importer GDP per capita (in logs)", y = "log(Domestic Price) - log(Import Price)") 


ggsave("statistics_and_graphs/import_vs_export_prices/plot_diff_med_weighted_asia.png", plot = plot_diff_med_weighted_asia)



